package com.weixing.mall.provider.controller.front;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.collect.Lists;
import com.weixing.mall.base.wrapper.Result;
import com.weixing.mall.base.wrapper.ResultUtil;
import com.weixing.mall.core.annotation.NoNeedAccess;
import com.weixing.mall.core.annotation.NoRepeatSubmit;
import com.weixing.mall.core.support.BaseController;
import com.weixing.mall.core.support.auth.MemAuth;
import com.weixing.mall.core.support.auth.MemInfo;
import com.weixing.mall.core.support.auth.SysAuth;
import com.weixing.mall.core.support.auth.SysInfo;
import com.weixing.mall.provider.api.model.dto.AfterOrderImgDto;
import com.weixing.mall.provider.model.dto.AfterSaleDto;
import com.weixing.mall.provider.model.enums.AfterSaleAuditEnum;
import com.weixing.mall.provider.model.query.AfterSaleAppQuery;
import com.weixing.mall.provider.model.vo.AfterSaleListVo;
import com.weixing.mall.provider.service.IAfterSaleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author wx
 * @Date 2019/9/26 17:00
 * @Version 1.0
 */
@RestController
@RequestMapping("front/afterSale")
@Api(value = "前端：售后/退款/换货API", tags = "前端：退款/换货")
public class FrontAfterSaleController extends BaseController {
    @Autowired
    private IAfterSaleService afterSaleService;

    @ApiOperation("退款/换货")
    @PostMapping("save")
    @NoRepeatSubmit(leaseTime = 10)
    public Result save(@MemInfo MemAuth auth, @RequestBody AfterSaleDto afterSaleDto) {
       /* boolean checkOrder = afterSaleService.checkOrder(afterSaleDto);//检查是否为换货订单，且为瑕疵选项
        if (!checkOrder) {
            return ResultUtil.error("图片信息检测是吧，请重试！");
        }*/
        afterSaleService.save(afterSaleDto, auth.getMid());
        return ResultUtil.success();
    }

    @ApiOperation(value = "售后退换货列表")
    @GetMapping("getAfterSaleList")
    public Result getAfterSaleList(@MemInfo MemAuth auth, AfterSaleAppQuery afterSaleAppQuery) {
        if (ObjectUtil.isNotEmpty(auth)) {
            afterSaleAppQuery.setMemberId(auth.getMid());
        }
        List<Integer> statuslist = Lists.newArrayList();
        if (ObjectUtil.isNotEmpty(afterSaleAppQuery) && afterSaleAppQuery.getStatus() != null && AfterSaleAuditEnum.FINISHED.getValue().equals(afterSaleAppQuery.getStatus().getValue())) {
            statuslist.add(AfterSaleAuditEnum.FINISHED.getValue());
        } else {
            statuslist.add(AfterSaleAuditEnum.WAIT_AUIT.getValue());
            statuslist.add(AfterSaleAuditEnum.PASS_AUIT.getValue());
            statuslist.add(AfterSaleAuditEnum.APP_REFUSE_AUIT.getValue());
            statuslist.add(AfterSaleAuditEnum.SEALED.getValue());
            statuslist.add(AfterSaleAuditEnum.WAIT_SHIP.getValue());
            statuslist.add(AfterSaleAuditEnum.SHIPED.getValue());
        }
        afterSaleAppQuery.setStatusList(statuslist);
        IPage<AfterSaleListVo> list = afterSaleService.getAfterSaleList(initMpPage(), afterSaleAppQuery);
        return ResultUtil.success(list);
    }

    @ApiOperation("查询退款详情")
    @PostMapping("refundDetails/{id}")
    //TODO 订单识别码没有查询
    public Result selectRefundDetails(@PathVariable long id) {
        return ResultUtil.success(afterSaleService.selectRefundDetails(id));
    }

    @ApiOperation("查询换货详情")
    @PostMapping("exchangeGoodsDetails/{id}")
    public Result selectExchangeGoodsDetails(@PathVariable long id) {
        return ResultUtil.success(afterSaleService.selectExchangeGoodsDetails(id));
    }

    @ApiOperation("查询退款成功详情")
    @PostMapping("refundSuccess/{id}")
    public Result selectRefundSuccess(@PathVariable long id) {
        return ResultUtil.success(afterSaleService.selectRefundSuccess(id));
    }

    @ApiOperation("查询换货成功详情")
    @PostMapping("exchangeGoodsSuccess/{id}")
    public Result selectExchangeGoodsSuccess(@PathVariable long id) {
        return ResultUtil.success(afterSaleService.selectExchangeGoodsSuccess(id));
    }

}
